package demo11;

import demo10.DefinePartitionJob;
import demo10.Subject;
import demo5.DescIntWritable;
import demo5.MyPartitioner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class TopNJob {

    public static void main(String[] args) throws Exception{
        // 1. 初始化配置
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://hadoop10:9000");
        //2. 创建job
        Job job = Job.getInstance(conf);
        job.setJarByClass(TopNJob.class);

        //3. 设置输入格式化工具和输出格式化
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        //4. 设置输入路径和输出路径
        TextInputFormat.addInputPath(job, new Path("/mapreduce/demo11/TOP3.txt"));
        TextOutputFormat.setOutputPath(job, new Path("/mapreduce/demo11/out"));

//        TextInputFormat.addInputPath(job, new Path(args[0]));
//        TextOutputFormat.setOutputPath(job, new Path(args[1]));
        //5. 设置mapper和reducer
        job.setMapperClass(TopNMapper.class);
        job.setReducerClass(TopNReducer.class);

        // 6. 设置mapper的kv类型和reducer的kv类型
        job.setMapOutputKeyClass(DescIntWritable.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(DescIntWritable.class);

        // 7. 启动job
        boolean b = job.waitForCompletion(true);
        System.out.println(b);
    }

    static class TopNMapper extends Mapper<LongWritable, Text, DescIntWritable,Text>{

        TreeMap<DescIntWritable,Text> treeMap = new TreeMap<>();

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] arr = value.toString().split("\t");
            treeMap.put(new DescIntWritable(Integer.parseInt(arr[1])),new Text(arr[0]));
            if (treeMap.size() > 3){
                treeMap.remove(treeMap.lastKey());
            }
        }

        // cleanup 晚于map方法执行，并且只执行一次

        @Override
        protected void cleanup(Context context) throws IOException, InterruptedException {
            Set<Map.Entry<DescIntWritable, Text>> set = treeMap.entrySet();
            for (Map.Entry<DescIntWritable, Text> entry : set) {
                context.write(entry.getKey(),entry.getValue());
            }
        }
    }

    static class TopNReducer extends Reducer<DescIntWritable,Text,Text,DescIntWritable>{
        int i = 0;
        @Override
        protected void reduce(DescIntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            if (i == 3){
                return;
            }
            for (Text text : values) {
                context.write(text,key);
                i++;
            }
        }
    }
}
